// pages/llgroup/llgroup.js
var Ajax = require('../../utils/Ajax.js')
var utils = require('../../utils/util.js');
var config = require('../../base.conf.js').config;
var timer = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodstag: [],
    pageSize: 15,
    currentPage: 1,
    statusType: 'none',
    statuserro: false,
    autoplay:false,
    banneList: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      userInfo: utils.getUserInfo(),
    }, () => {
      this.refresh();
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.PlAdvertisLike()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
     this.setData({
       autoplay: true,
     })
  },
  PlAdvertisLike() {
    var that = this
    Ajax.post("PlAdvertisLike", {
      area: "1", //平台
      position: "12", //积分换轮播
    }).then((d) => {
      utils.hideLoading()
      console.log(d)
      var imglist = d.data
      if (d.status == 200) {
        that.setData({
          banneList: d.data
        })
      } else {
        utils.showtoast(d.msg)
      }
    }).catch((e) => {
      console.log(e)
      utils.hideLoading()
      utils.showtoast('网络错误,数据加载失败')
    })
  },
  refresh() { //下拉刷新
    var {
      moreType,
      currentPage,
    } = this.data;
    if (moreType != "loading") {
      this.setData({
        currentPage: 1,
        moreType: 'none'
      }, () => {
        this.purchase();
      })
    }
  },
  gotoGoods(e) {
    var {
      productId,
      integral
    } = e.detail
    utils.toPage(`/pages/goodsmain/goodsmain?productId=${productId}&onlineType=2&ischang=${integral ? 1 : 0}`)
  },
  purchase() { //查询商品列表
    var {
      pageSize,
      currentPage,
      options,
      moreType,
      changatv,
      userInfo
    } = this.data;
    Ajax.get("goodslist", {
      pageSize,
      currentPage,
      type: 2
    }).then((d) => {
      utils.hideLoading();
      wx.stopPullDownRefresh();
      if (d.status == 200) {
        moreType = "showText";
        var list = d.data && d.data.list || [];
        // list.map((item) => {
        //   var strattime = new Date().getTime(); //现在的时间
        //   var endtimes = item.iffDate + strattime;
        //   if (!item.iffday && item.iffDate) {
        //     item.timesArr = this.dateDif(item.iffDate) || [null, null, null, null, 0];
        //     item.iffDate = Number(item.iffDate) - 1000 > 0 ? Number(item.iffDate) - 1000 : 0;
        //     item.state = (Number(item.iffDate) - 1000 > 0 ? 1 : 0)
        //   } else {
        //     item.iffDate = item.iffDate || 0;
        //     item.state = item.iffDate ? 1 : 0;
        //   }
        // })
        list.length < pageSize ? moreType = "noMore" : moreType
        if (currentPage != 1) {
          list = this.data.goodstag.concat(list);
        }
        this.setData({
          goodstag: list,
          statusType: list.length > 0 ? 'none' : 'empty',
          statuserro: false,
          moreType
        }, () => {
          // this.onEndtime()
        })
      } else {
        var {
          goodstag
        } = this.data;
        this.setData({
          moreType: "none",
          statusType: goodstag.length > 0 ? 'none' : 'empty',
          statuserro: goodstag.length > 0 ? false : true,
        })
        utils.showtoast(d.msg || d.data || '服务器错误,请稍后重试')
      }
    }).catch((e) => {
      console.log(e)
      utils.hideLoading();
      wx.stopPullDownRefresh();
      var {
        goodstag
      } = this.data;
      this.setData({
        moreType: "none",
        statusType: goodstag.length > 0 ? 'none' : 'empty',
        statuserro: goodstag.length > 0 ? false : true,
      })
      utils.showtoast('网络错误，请检查您的网络')
    })
  },
  onEndtime() {
    clearInterval(timer);
    timer = null;
    var {
      goodstag
    } = this.data;
    if (goodstag.length > 0) {
      timer = setInterval(() => {
        goodstag.map((item, index) => {
          var temp = 'goodstag' + '[' + index + '].timesArr'
          var state = 'goodstag' + '[' + index + '].state'
          var endtimes = 'goodstag' + '[' + index + '].iffDate'
          if (!item.iffday && item.iffDate) {
            var timesArr = (item.iffDate && this.dateDif(item.iffDate)) || [null, null, null, null];
            this.setData({
              [temp]: timesArr,
              [endtimes]: Number(item.iffDate) - 1000 > 0 ? Number(item.iffDate) - 1000 : 0,
              [state]: (Number(item.iffDate) - 1000 > 0 ? 1 : 0)
            })
          } else {
            this.setData({
              [endtimes]: item.iffDate ? item.iffDate : 0,
              [state]: item.iffDate ? 1 : 0
            })
          }
        })
      }, 1000);
    }
  },
  dateDif(date) { //处理倒计时
    if (date <= 0) {
      return null;
    }
    var days = date / 1000 / 60 / 60 / 24;
    var daysRound = Math.floor(days); //对一个数进行下舍入。
    var hours = date / 1000 / 60 / 60 - (24 * daysRound);
    var hoursRound = Math.floor(hours);
    var minutes = date / 1000 / 60 - (24 * 60 * daysRound) - (60 * hoursRound);
    var minutesRound = Math.floor(minutes);
    var seconds = date / 1000 - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound);
    var secondsRound = Math.floor(seconds);
    return [this.addZero(daysRound), this.addZero(hoursRound), this.addZero(minutesRound), this.addZero(secondsRound)];
  },
  addZero(numbers) { //小于10前面加一个零
    if (numbers < 10) {
      return "0" + numbers
    }
    return numbers;
  },
  onReachBottom: function() { //滚动触底加载
    var {
      moreType,
      currentPage,
    } = this.data;
    if (moreType == "showText") {
      var currentPage = currentPage + 1;
      this.setData({
        currentPage,
        moreType: "loading"
      }, () => {
        this.purchase();
      });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      autoplay: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    utils.showLoading();
    this.refresh();
  }
})